﻿using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;
using WebApplication3.Data;
using WebApplication3.Model;

namespace WebApplication3.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly ApplicationDbContext _context;

        public UserController(ApplicationDbContext context)
        {
            _context = context;
        }

        [HttpPost("resetPassword")]
        public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordRequest request)
        {
            if (request == null || string.IsNullOrEmpty(request.UserId) || string.IsNullOrEmpty(request.Password) || string.IsNullOrEmpty(request.Username))
            {
                return BadRequest(new { code = 400, message = "Invalid request." });
            }

            var user = _context.User.FirstOrDefault(u => u.UserId == request.UserId && u.Username == request.Username);
            if (user == null)
            {
                return NotFound(new { code = 404, message = "User not found." });
            }

            user.Password = request.Password;
            await _context.SaveChangesAsync();

            return Ok(new { code = 200, message = "Password reset successfully." });
        }

        public class ResetPasswordRequest
        {
            public int Id { get; set; }
            public string UserId { get; set; }
            public string Password { get; set; }
            public string Username { get; set; }
        }
    }
}
